package de.fhtrier.mp.wollmaus.entity;

import java.awt.Rectangle;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Sound;

import de.fhtrier.mp.wollmaus.constants.Constants;
import de.fhtrier.mp.wollmaus.util.AssetManager;

public class Note {
	private int x, y;
	private int id;
	private Animation ani;
	private Sound successSound;
	private Sound failSound;

	private final static int BUFFER_X = 15;
	private final static int BUFFER_Y = 18;

	private boolean collected;

	public Note(int x, int y, int id) {
		this.x = x;
		this.y = y;

		this.id = id;

		AssetManager am = AssetManager.getInstance();
		switch (id) {
		default:
			ani = am.getAnimation(AssetManager.IMAGE_NOTE0);
			successSound = am.getSound(AssetManager.SOUND_NOTE_D1);
			failSound = am.getSound(AssetManager.SOUND_NOTE_D2);
			break;
		case 1:
			ani = am.getAnimation(AssetManager.IMAGE_NOTE1);
			successSound = am.getSound(AssetManager.SOUND_NOTE_F1);
			failSound = am.getSound(AssetManager.SOUND_NOTE_F2);
			break;
		case 2:
			ani = am.getAnimation(AssetManager.IMAGE_NOTE2);
			successSound = am.getSound(AssetManager.SOUND_NOTE_A1);
			failSound = am.getSound(AssetManager.SOUND_NOTE_A2);
			break;
		case 3:
			ani = am.getAnimation(AssetManager.IMAGE_NOTE3);
			successSound = am.getSound(AssetManager.SOUND_NOTE_D1);
			failSound = am.getSound(AssetManager.SOUND_NOTE_D2);
			break;
		}
	}

	public void render(GameContainer gc, Graphics g) {
		if (!collected) {
			// img.draw(x,y-img.getHeight());
			ani.draw(x, y - ani.getHeight());

			if (Constants.debug == true) {
				Rectangle a = getCollisionRect();
				g.setColor(Color.cyan);
				g.fillRect(a.x, a.y, a.width, a.height);
			}
		}
	}

	public Rectangle getCollisionRect() {
		return new Rectangle(x + BUFFER_X, y - ani.getHeight() + BUFFER_Y, ani.getWidth() - (BUFFER_X * 2), ani.getHeight() - (BUFFER_Y * 2));
	}

	public int getId() {
		return id;
	}

	public boolean isCollected() {
		return collected;
	}

	public void setCollected(boolean collected) {
		this.collected = collected;
	}

	public boolean isCollectable(Player player) {
		return getId() == player.getCollectedNotes();
	}

	public void collectSucces() {
		successSound.play(1f, Constants.SOUND_VOLUME);
	}

	public void collectFail() {
		failSound.play(1f, Constants.SOUND_VOLUME);
	}
}
